var api = require('../../../../api.js');
var utils = require('../../../../utils.js');
// var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        series_flag:true,
    },
    // submit changed form
    form_submit:function(t){
        console.log(t.detail.value)
        let form_value = t.detail.value;
        let that = this;
        let id = that.data.id;
        let series_id = that.data.series_id;
        let model_id = that.data.model_id;
        app.request({
            url: api.car.submit,
            data:{
                id:id,
                buy_time: form_value.buy_time,
                engine_num: form_value.engine_num,
                frame_num: form_value.frame_num,
                model_id: model_id,
                owner: form_value.owner,
                plate_num: form_value.plate_num,
                price: form_value.price,
                series_id: series_id,
                total_mileage: form_value.total_mileage,
            },
            method:"POST",
            success: function(res) {
                console.log(res)
                if(res.code==0){
                    wx.redirectTo({
                        url: '/page/carbarn/pages/car_list/car_list',                        
                    })
                }
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },
 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;  
        console.log(options)
        // return;
        let id = options.id;
        let car_name = options.car_name;
        let model_id = options.model_id;
        let series_id = options.series_id;

        that.setData({
            car_name: car_name,
            id: id,
            series_id: series_id,
            model_id: model_id
        })
        app.request({
            url: api.car.my_form,
            data:{id:id},
            method: 'GET',
            success: function(res) {
                console.log(res.data[0])
                let carbarn_list = res.data;
                that.setData({
                    carbarn_list: res.data,
                    detail: res.data[0]
                })

                app.request({
                    url: api.car.series,
                    method: 'GET',
                    success: function (res) {
                        console.log(res[0])
                        let car_lsit = res[0];
                        that.setData({
                            _list: res[0]
                        })
                        var series_list = [];                        
                        for (var i = 0; i < car_lsit.length; i++) {
                            series_list.push(car_lsit[i].series_name);
                            console.log(car_lsit[i].id);  

                            // var series_obj = new Object();
                            // series_obj.id = car_lsit[i].id;
                            // series_obj.series_name = car_lsit[i].series_name;
                            // series_list.push(series_obj);
                        }

                        that.setData({
                            series_list: series_list
                        })
                        // 定义一个空数组  添加到循环中 使用
                        var ser_list = [];
                        for (var j = 0; j < carbarn_list.length; j++) {
                            // console.log(carbarn_list[j].id)
                            for (var i = 0; i < car_lsit.length; i++) {
                                if (car_lsit[i].id == carbarn_list[j].series_id) {
                                    console.log(car_lsit[i].series_name)
                                    console.log(car_lsit[i].cover_pic)
                                    console.log(carbarn_list[j].plate_num)

                                    var obj = new Object();
                                    obj.name = car_lsit[i].series_name;
                                    obj.logo = car_lsit[i].cover_pic;
                                    // obj.id = carbarn_list[i].series_id,
                                    // obj.plate_num = carbarn_list[j].plate_num;

                                    ser_list.push(obj)
                                }
                            }
                        }
                        console.log(ser_list);
                        ser_list = ser_list.reverse()
                        that.setData({
                            ser_list: ser_list
                        })
                    },
                })

            },
        })
    },



    //choose series_car
    series_car: function (e) {
        let that = this;
        let series_flag = !that.data.series_flag
        that.setData({
            series_flag: false
        })
        let car_list = that.data._list;
        let seriesr_id = car_list[e.detail.value].id;
        console.log(seriesr_id)
        console.log("=================================================")
        let keyword = car_list[e.detail.value].series_name;
        console.log(keyword)
        that.setData({
            series_inndex: e.detail.value,
            seriesr_id: seriesr_id,
            keyword: keyword
        })

        // Top choose series_car
        app.request({
            url: api.car.model,
            data: {
                keyword: keyword
            },
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
                console.log(res[0])

                // model_list
                let md_list = [];
                let model_list = res[0];
                for (var j = 0; j < model_list.length; j++) {
                    md_list.push(model_list[j].model_name)
                    console.log(model_list[j].model_name)
                }
                console.log(md_list)
                that.setData({
                    model_list: res[0],
                    md_list: md_list
                })
            },
        })




    },
    // choose model
    model_list: function (e) {
        let that = this;
        console.log(e)
        let model_index = e.detail.value
        let series_id = that.data.seriesr_id;
        let model_id = that.data.model_list[model_index].id;
        console.log(series_id)
        console.log(model_id)
        console.log('model_id+++++++++++++++++++++++++++')
        console.log("=+++++++++++++++++++++")
        that.data.md_list
        console.log(that.data.md_list)
        that.setData({
            model_index: model_index,
            model_id: model_id,
            series_id: series_id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        let that = this;
    
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})